package pers.jphoebe.quartz.config.okhttp;

import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

/**
 * Created by 蒋时华 on 2017/9/29.
 */
@Configuration
public class OkHttpConfig {

    @Value("${OkHttp.config.connectTime}")
    private Integer connectTime;
    @Value("${OkHttp.config.readTime}")
    private Integer readTime;
    @Value("${OkHttp.config.writeTime}")
    private Integer writeTime;
    @Value("${OkHttp.config.retryOnConnectionFailure}")
    private boolean retryOnConnectionFailure;

    @Bean
    public OkHttpClient okHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(this.connectTime, TimeUnit.SECONDS)
                .readTimeout(this.readTime, TimeUnit.SECONDS)
                .writeTimeout(this.writeTime,TimeUnit.SECONDS)
                .retryOnConnectionFailure(this.retryOnConnectionFailure);
                // 配置Interceptor
//                .addInterceptor(new Interceptor() {
//                    @Override
//                    public Response intercept(Chain chain) throws IOException {
//                        GsiPrice originalRequest = chain.request();
//                        GsiPrice requestWithUserAgent = originalRequest.newBuilder()
//                                .header(USER_AGENT_HEADER_NAME, CUSTOM_USER_AGENT)
//                                .build();
//                        return chain.proceed(requestWithUserAgent);
//                    }
//                });
                // 配置 https
//                .sslSocketFactory(getTrustedSSLSocketFactory())
//                .hostnameVerifier(DO_NOT_VERIFY);

        return builder.build();
    }

    TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    X509Certificate[] x509Certificates = new X509Certificate[0];
                    return x509Certificates;
                }

                public void checkClientTrusted(
                        X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(
                        X509Certificate[] certs, String authType) {
                }
            }
    };

    HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };

    private SSLSocketFactory getTrustedSSLSocketFactory() {
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            return sc.getSocketFactory();
        } catch (KeyManagementException | NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }


}
